// MessageSender.java

package com.sateh.spaces.examples;

import com.sateh.spaces.Space;
import com.sateh.spaces.SpaceException;
import com.sateh.spaces.UnusableEntryException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MessageSender implements Runnable
{
    private static final Log log = LogFactory.getLog(MessageSender.class);

    private Space space;

    public MessageSender(Space space)
    {
        this.space = space;
    }

    public void run()
    {
        while (true)
        {
            try
            {
                log.debug("Thread " + this.toString()+ ": Taking a Message");
                Message message = space.take(Message.class, null, Long.MAX_VALUE);
                log.debug("Thread " + this.toString()+ ": Sending <" + message.getContent() + "> to <" + message.getRecipient() + ">");

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException exception) {
                    // Ignored
                }
            }

            catch (SpaceException e)
            {
                log.error("Failed to take a message: " + e.getMessage(), e);
            } catch (UnusableEntryException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}
